package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.Permission;
import com.ejuornal.bean.RolePermission;
import com.ejuornal.bean.vo.PermissionVO;
import com.ejuornal.dao.PermissionDao;
@Component("permissionDao")
public class PermissionDaoImpl extends DaoSupport<Permission> implements PermissionDao{

    @Override
    public void add(String sql, Object... objs) {
        // TODO Auto-generated method stub
        super.add(sql, objs);
    }

    @Override
    public void update(String sql, Object... objs) {
        // TODO Auto-generated method stub
        super.update(sql, objs);
    }

    @Override
    public Page<Permission> find(int current_record, String url, String hsql, Object[] objs) {
        // TODO Auto-generated method stub
        return super.find(current_record, url, hsql, objs);
    }

    public boolean delete(int id) {
        String sql = "delete from t_permission where pid=?";
        int num = super.getJdbcTemplate().update(sql, id);
        if(num == 1) {
            return true;
        }
        return false;
    }

	@Override
	public boolean batchUpdate() {
//		final List<String> p = new ArrayList<String>();
//		pgNames.add("会员列表");
//		pgNames.add("主编未审核");
//		pgNames.add("");
//		pgNames.add("期刊管理");
//		pgNames.add("权限管理");
//		pgNames.add("系统管理");
//		pgNames.add("广告管理");
//		pgNames.add("期刊分类管理");
//		pgNames.add("评论管理");
//		String sql ="insert into t_permission_group(pgId, pgName) value(?,?)";
//		getJdbcTemplate().batchUpdate(sql, 
//				 new BatchPreparedStatementSetter(){
//					@Override
//					public int getBatchSize() {
//						return 0;
//					}
//					@Override
//					public void setValues(PreparedStatement ps, int i)
//							throws SQLException {
//						ps.setInt(1, i+1);
//						ps.setString(2, pgNames.get(i));
//					}
//				}
//				);
		return true;
	}

	@Override
	public List<Permission> findByRoleId(int roleId) {
		String sql = "select t1.pname, t1.pid, t1.pdesc from t_permission t1, t_role_permission t2 where t2.roleId=? and t1.pid=t2.pid";
		@SuppressWarnings({ "unchecked", "rawtypes" })
		List<Permission> permissions = getJdbcTemplate().query(sql, new Object[]{roleId},
				new RowMapper() {
					public Object mapRow(ResultSet rs, int arg1)
							throws SQLException {
						Permission p = new Permission();
						p.setPid(rs.getInt("pid"));
						p.setPname(rs.getString("pname"));
						p.setPdesc(rs.getString("pdesc"));
						return p;
					}
			
				}
		);
		return permissions;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Permission> findByNotIntRole(int roleId) {
		String sql = "select * from t_permission t1 where t1.pid not in (SELECT t2.pid from t_role_permission t2 where t2.roleId=?)";
		List<Permission> permissions = getJdbcTemplate().query(sql,new Object[]{roleId},
				new RowMapper() {
			public Object mapRow(ResultSet rs, int arg1)
					throws SQLException {
				Permission p = new Permission();
				p.setPid(rs.getInt("pid"));
				p.setPname(rs.getString("pname"));
				p.setPdesc(rs.getString("pdesc"));
				return p;
			}
	
		} );
		return permissions;
	}

	@Override
	public boolean addRolePermission(RolePermission rp) {
		String sql = "insert into t_role_permission(roleId, pid) value(?, ?)";
		int num = getJdbcTemplate().update(sql, new Object[]{rp.getRoleId(), rp.getPid()});
		if(num > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<Permission> findByPgId(int pgid, int roleId) {
		String sql = "select t1.* from t_permission t1, t_role_permission t2 where t1.pgId=? and t2.pid = t1.pid and t2.roleId=?";
		List<Permission> p = getJdbcTemplate().query(sql, new Object[]{pgid, roleId},
				new RowMapper() {

					@Override
					public Object mapRow(ResultSet rs, int arg1)
							throws SQLException {
						Permission p = new Permission();
						p.setPid(rs.getInt("pid"));
						p.setPname(rs.getString("pname"));
						p.setPdesc(rs.getString("pdesc"));
						return p;
					}
			
		}
				);
		return p;
	}


}
